package com.fridge.fridgeList;

import java.io.File;
import java.io.InputStream;
import java.util.List;

import com.fridge.data.FridgeItem;
import com.id2216.fridge.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class FridgeAdapter extends ArrayAdapter<FridgeItem> {

    Context context;
    int layoutResourceId;   
    List<FridgeItem> data = null;
    
	public FridgeAdapter(Context context, int resource, List<FridgeItem> fridgeItemList) 
	{
		super(context, resource, fridgeItemList);
		
        this.layoutResourceId = resource;
        this.context = context;
        this.data = fridgeItemList;
 	}
	
	
	
//	@Override
//    public View getView(int position, View convertView, ViewGroup parent) {
//        View row = convertView;
//        FridgeHolder holder = null;
//        FridgeItem item = data.get(position);
//        Bitmap image = null;
//                
//        if ((item != null) &&   (item.getPic().compareTo("") != 0))
//        {        	
//        	BitmapFactory.Options options = new BitmapFactory.Options();
//        	options.inSampleSize = 8;
//        	try
//        	{
//                image = BitmapFactory.decodeFile(item.getPic(), options);  
//        	}
//        	catch(Exception e)
//        	{
//        		
//        	}
//      	        		
//        	if(image != null )        	
//        	{
//        		if( row == null)
//        		{
//                    LayoutInflater inflater = ((Activity)context).getLayoutInflater();
//                    row = inflater.inflate(R.layout.fridge_item_cell_image, parent, false);
//                    holder = new FridgeHolder();
//                    holder.imgIndicator = (ImageView)row.findViewById(R.id.expireIndicator);
//                    holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
//                    holder.txtExpire = (TextView)row.findViewById(R.id.txtExpire);
//                    holder.imgDelete = (ImageView)row.findViewById(R.id.imageDelete);     
//                    holder.imgFridgeItem = (ImageView)row.findViewById(R.id.fridgeItemImage);
//
//                    row.setTag(holder);
//        		}
//                else
//                {
//                    holder = (FridgeHolder)row.getTag();
//                }
//        		
//                holder.txtTitle.setText(item.getName());
//                holder.txtExpire.setText(item.getExpireDay());
//                
//                // Set text style and color
//                if(item.getExpireDayCount() < 0 )
//                {	
//                	holder.txtTitle.setTextColor(Color.parseColor("#800000"));
//                	holder.txtTitle.setTypeface(null,Typeface.BOLD);
//                	holder.txtExpire.setTextColor(Color.parseColor("#800000"));
//                	holder.txtExpire.setTypeface(null,Typeface.BOLD);        	
//                	holder.imgIndicator.setImageResource(R.drawable.red_block);
//                }
//                else if(item.getExpireDayCount() == 0)
//                {
//                	holder.txtTitle.setTextColor(Color.parseColor("#e07d3c"));
//                	holder.txtExpire.setTextColor(Color.parseColor("#e07d3c"));
//                	holder.imgIndicator.setImageResource(R.drawable.orange_block);
//                }
//                else
//                {
//                    holder.txtTitle.setTextColor(Color.BLACK);
//                	holder.txtTitle.setTypeface(null,Typeface.NORMAL);
//                    holder.txtExpire.setTextColor(Color.BLACK);
//                	holder.txtExpire.setTypeface(null,Typeface.NORMAL);
//                	holder.imgIndicator.setImageResource(R.drawable.green_block);
//                	
//                }
//                
//                // set Editable
//                holder.imgDelete.setVisibility(item.getUiStatus());
//                if(holder.imgFridgeItem != null )
//                {
//                    if( image != null )
//                    	holder.imgFridgeItem.setImageBitmap(image);
//                    holder.imgFridgeItem.setScaleType(ImageView.ScaleType.CENTER_CROP);
//                    holder.imgFridgeItem.setPadding(8, 8, 8, 8);
//                	
//                }
//        	}        	
//        }
//        
//        /// ------ TEXT CELL ------------
//        if(image == null)
//        {
//            if(row == null)
//            {
//                LayoutInflater inflater = ((Activity)context).getLayoutInflater();
//                row = inflater.inflate(layoutResourceId, parent, false);
//               
//                holder = new FridgeHolder();
//                holder.imgIndicator = (ImageView)row.findViewById(R.id.expireIndicator);
//                holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
//                holder.txtExpire = (TextView)row.findViewById(R.id.txtExpire);
//                holder.imgDelete = (ImageView)row.findViewById(R.id.imageDelete);
//                
//                
//                row.setTag(holder);
//            }
//            else
//            {
//                holder = (FridgeHolder)row.getTag();
//            }
//           
//            holder.txtTitle.setText(item.getName());
//            holder.txtExpire.setText(item.getExpireDay());
//            
//            // Set text style and color
//            if(item.getExpireDayCount() < 0 )
//            {	
//            	holder.txtTitle.setTextColor(Color.parseColor("#800000"));
//            	holder.txtTitle.setTypeface(null,Typeface.BOLD);
//            	holder.txtExpire.setTextColor(Color.parseColor("#800000"));
//            	holder.txtExpire.setTypeface(null,Typeface.BOLD);        	
//            	holder.imgIndicator.setImageResource(R.drawable.red_block);
//            }
//            else if(item.getExpireDayCount() == 0)
//            {
//            	holder.txtTitle.setTextColor(Color.parseColor("#e07d3c"));
//            	holder.txtExpire.setTextColor(Color.parseColor("#e07d3c"));
//            	holder.imgIndicator.setImageResource(R.drawable.orange_block);
//            }
//            else
//            {
//                holder.txtTitle.setTextColor(Color.BLACK);
//            	holder.txtTitle.setTypeface(null,Typeface.NORMAL);
//                holder.txtExpire.setTextColor(Color.BLACK);
//            	holder.txtExpire.setTypeface(null,Typeface.NORMAL);
//            	holder.imgIndicator.setImageResource(R.drawable.green_block);
//            }
//            
//            // set Editable
//            holder.imgDelete.setVisibility(item.getUiStatus());
//            	
//        }
//        
//        return row;
//
//    }

	
	
	
	
	
	
	
	
	
	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        FridgeHolder holder = null;
        FridgeItem item = data.get(position);
        Bitmap image = null;
                
        if (item.getPic().compareTo("") != 0)
        {        	
        	BitmapFactory.Options options = new BitmapFactory.Options();
        	options.inSampleSize = 8;
        	try
        	{
                image = BitmapFactory.decodeFile(item.getPic(), options);  
        	}
        	catch(Exception e)
        	{
        		
        	}
        }	
      	        		
        if( row == null)
        {
        	LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        	row = inflater.inflate(R.layout.fridge_item_cell_image, parent, false);
        	holder = new FridgeHolder();
        	holder.imgIndicator = (ImageView)row.findViewById(R.id.expireIndicator);
        	holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
        	holder.txtExpire = (TextView)row.findViewById(R.id.txtExpire);
        	holder.imgDelete = (ImageView)row.findViewById(R.id.imageDelete);     
        	holder.imgFridgeItem = (ImageView)row.findViewById(R.id.fridgeItemImage);

        	row.setTag(holder);
        }
        else
        {
        	holder = (FridgeHolder)row.getTag();
        }
        		
        holder.txtTitle.setText(item.getName());
        holder.txtExpire.setText(item.getExpireDay());
                
        // Set text style and color
        if(item.getExpireDayCount() < 0 )
        {	
        	holder.txtTitle.setTextColor(Color.parseColor("#800000"));
        	holder.txtTitle.setTypeface(null,Typeface.BOLD);
        	holder.txtExpire.setTextColor(Color.parseColor("#800000"));
        	holder.txtExpire.setTypeface(null,Typeface.BOLD);        	
        	holder.imgIndicator.setImageResource(R.drawable.red_block);
        }
        else if(item.getExpireDayCount() == 0)
        {
        	holder.txtTitle.setTextColor(Color.parseColor("#e07d3c"));
        	holder.txtExpire.setTextColor(Color.parseColor("#e07d3c"));
        	holder.imgIndicator.setImageResource(R.drawable.orange_block);
        }
        else
        {
        	holder.txtTitle.setTextColor(Color.BLACK);
        	holder.txtTitle.setTypeface(null,Typeface.NORMAL);
        	holder.txtExpire.setTextColor(Color.BLACK);
        	holder.txtExpire.setTypeface(null,Typeface.NORMAL);
        	holder.imgIndicator.setImageResource(R.drawable.green_block);
                	
        }
                
        // set Editable
        holder.imgDelete.setVisibility(item.getUiStatus());
                
        if(image != null )
        {
        	holder.imgFridgeItem.setVisibility(View.VISIBLE);
        	holder.imgFridgeItem.setImageBitmap(image);
        	holder.imgFridgeItem.setScaleType(ImageView.ScaleType.CENTER_CROP);
        	holder.imgFridgeItem.setPadding(8, 8, 8, 8);
	    }
        else
        	holder.imgFridgeItem.setVisibility(View.GONE);
        
        return row;

    }

	
	
	static class FridgeHolder
	{
		//ImageView buttonDelete; 
		ImageView imgDelete;
		ImageView imgIndicator;
		TextView txtTitle;
		TextView txtExpire;
		ImageView imgFridgeItem;
	}

}
